<?php

/**
 * 订单列表
 */
namespace app\mall\api;

class OrderApi extends \app\member\api\MemberApi {

    /**
     * 订单列表
     */
    public function index() {

        $where = [];
        $type = intval($this->data['type']);
        if ($type) {
            $where['B.order_status'] = 1;
        }
        $urlParam['type'] = $type;
        switch ($type) {
            case 1:
                $where['B.pay_type'] = 1;
                $where['B.pay_status'] = 0;
                $where['delivery_status'] = 0;
                break;
            case 2:
                $where['_sql'][] = '(B.pay_type = 0 OR B.pay_status = 1)';
                $where['delivery_status'] = 0;
                break;
            case 3:
                $where['delivery_status'] = 1;
                $where['order_complete_status'] = 0;
                break;
            case 4:
                $where['order_complete_status'] = 1;
                $where['comment_status'] = 0;
                break;
        }

        $where['B.order_user_id'] = $this->userId;

        $pageLimit = $this->data['limit'] ? $this->data['limit'] : 10;


        $model = target('mall/MallOrder');
        $count = $model->countList($where);
        $pageData = $this->pageData($count, $pageLimit);
        $list = $model->loadList($where, $pageData['limit'], 'B.order_id desc');

        $orderIds = [];
        foreach ($list as $key => $vo) {
            $orderIds[] = $vo['order_id'];
        }
        if (!empty($orderIds)) {
            $orderGoods = target('order/OrderGoods')->loadList([
                '_sql' => 'order_id in (' . implode(',', $orderIds) . ')'
            ]);
            $orderGroup = [];
            foreach ($orderGoods as $key => $vo) {
                $orderGroup[$vo['order_id']][] = $vo;
            }
            foreach ($list as $key => $vo) {
                $list[$key]['order_items'] = $orderGroup[$vo['order_id']];
            }
        }

        if ($list) {
            $this->success('ok', [
                'data' => $list,
                'pageData' => [
                    'pageLimit' => $pageLimit,
                    'limit' => count($list),
                    'page' => $pageData['page'],
                    'totalPage' => $pageData['totalPage']
                ]
            ]);
        } else {
            $this->error('暂无更多订单', 404);
        }

    }

    /**
     * 详情
     */
    public function info() {

        $orderNo = $this->data['order_no'];
        if (empty($orderNo)) {
            $this->error404();
        }
        $orderInfo = target('mall/MallOrder')->getWhereInfo([
            'B.order_no' => $orderNo
        ]);
        if (empty($orderInfo)) {
            $this->error404();
        }
        if ($orderInfo['order_user_id'] <> $this->userId) {
            $this->error404();
        }

        $payInfo = [];
        if ($orderInfo['pay_status']) {
            $payInfo = target('member/PayLog')->getInfo($orderInfo['pay_id']);
        }
        $deliveryInfo = [];
        if ($orderInfo['delivery_status']) {
            $deliveryInfo = target('order/OrderDelivery')->getWhereInfo([
                'A.order_id' => $orderInfo['order_id']
            ]);
        }

        $step = 1;
        if ($orderInfo['pay_type']) {
            if ($orderInfo['status_data']['action'] == 'pay') {
                $step = 1;
            }
            if ($orderInfo['status_data']['action'] == 'delivery' || $orderInfo['status_data']['action'] == 'parcel') {
                $step = 2;
            }
            if ($orderInfo['status_data']['action'] == 'receive') {
                $step = 3;
            }
            if ($orderInfo['status_data']['action'] == 'complete'|| $orderInfo['status_data']['action'] == 'comment') {
                $step = 4;
            }
        }else {
            if ($orderInfo['status_data']['action'] == 'delivery' || $orderInfo['status_data']['action'] == 'parcel') {
                $step = 1;
            }
            if ($orderInfo['status_data']['action'] == 'receive') {
                $step = 2;
            }
            if ($orderInfo['status_data']['action'] == 'pay') {
                $step = 3;
            }
            if ($orderInfo['status_data']['action'] == 'complete' || $orderInfo['status_data']['action'] == 'comment') {
                $step = 4;
            }
        }

        $orderGoods = target('order/OrderGoods')->loadList([
            'order_id' => $orderInfo['order_id']
        ]);

        $logList = target('order/OrderLog')->loadList(['order_id' => $orderInfo['order_id'], 'type' => 1]);

        $this->success('ok', [
            'payInfo' => $payInfo,
            'deliveryInfo' => $deliveryInfo,
            'info' => $orderInfo,
            'orderGoods' => $orderGoods,
            'orderStep' => $step,
            'logList' => $logList
        ]);
    }


}